package com.antis.olsl.library.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.antis.olsl.library.R;
import com.antis.olsl.library.common.ConstantsPool;
import com.antis.olsl.library.widget.wheelview.WheelView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间选择控件
 * Created by qi.yang on 2018/6/3 0003.
 */

public class ChooseDateDialog {

    private ArrayList<String> yearTexts = new ArrayList<>();//用于控件显示的数组
    private ArrayList<String> quarterTexts = new ArrayList<>();
    private ArrayList<String> monthTexts = new ArrayList<>();
    private ArrayList<String> weekTexts = new ArrayList<>();
    private ArrayList<String> dayTexts = new ArrayList<>();

    private ArrayList<Integer> years = new ArrayList<>();//实际int值
    private ArrayList<Integer> quarters = new ArrayList<>();
    private ArrayList<Integer> months = new ArrayList<>();
    private ArrayList<Integer> weeks = new ArrayList<>();
    private ArrayList<Integer> days = new ArrayList<>();

    private Dialog dateDialog;
    private Context context;
    private String title = "";//弹窗标题
    private Date selectedDate;//被选择时间
    private Calendar calendar;

    private int yearSelectedIndex = 0;
    private int quarterSelectedIndex = 0;
    private int monthSelectedIndex = 0;
    private int weekSelectedIndex = 0;
    private int daySelectedIndex = 0;

    private ChooseDateListener chooseDateListener;
    private String tag;
    private WheelView yearWheelView;
    private WheelView quarterWheelView;
    private WheelView monthWheelView;
    private WheelView weekWheelView;
    private WheelView dayWheelView;

    private int mWeekPosition;
    private int mQuarterPosition;
    private int mType;

    public ChooseDateDialog(Context context, String title, Date selectedDate, ChooseDateListener chooseDateListener, String tag) {
        this.context = context;
        this.title = title;
        this.selectedDate = selectedDate;
        this.chooseDateListener = chooseDateListener;
        this.tag = tag;
        initData();
        initView();
    }

    public ChooseDateDialog(Context context, String title, Date selectedDate, ChooseDateListener chooseDateListener, String tag, int weekPosition, int quarterPosition, int type) {
        this.context = context;
        this.title = title;
        this.selectedDate = selectedDate;
        this.chooseDateListener = chooseDateListener;
        this.tag = tag;
        this.mWeekPosition = weekPosition;
        this.mQuarterPosition = quarterPosition;
        this.mType = type;
        initData();
        initView();
    }

    public ChooseDateDialog(Context context, String title, ChooseDateListener chooseDateListener, String tag) {
        this.context = context;
        this.title = title;
        this.chooseDateListener = chooseDateListener;
        this.tag = tag;
        initData();
        initView();
    }

    private void initView() {
        if (dateDialog != null) {
            dismiss();
        }
        dateDialog = new Dialog(context, R.style.dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_choose_date, null);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvSure = view.findViewById(R.id.tv_sure);
        yearWheelView = view.findViewById(R.id.wheel_view_year);
        quarterWheelView = view.findViewById(R.id.wheel_view_quarter);
        monthWheelView = view.findViewById(R.id.wheel_view_month);
        weekWheelView = view.findViewById(R.id.wheel_view_week);
        dayWheelView = view.findViewById(R.id.wheel_view_day);
        dateDialog.setContentView(view);
        view.findViewById(R.id.ll_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dateDialog.dismiss();
            }
        });

        tvTitle.setText(title);

        yearWheelView.setItems(yearTexts, getYearSelectedIndex());
        quarterWheelView.setItems(quarterTexts, getQuarterSelectedIndex());
        monthWheelView.setItems(monthTexts, getMonthSelectedIndex());
        weekWheelView.setItems(weekTexts, getWeekSelectedIndex());
        dayWheelView.setItems(dayTexts, getDaySelectedIndex());

        yearWheelView.setOnItemSelectedListener(new WheelView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int selectedIndex, String item) {
                yearSelectedIndex = selectedIndex;
                flushDate();
            }
        });

        quarterWheelView.setOnItemSelectedListener(new WheelView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int selectedIndex, String item) {
                quarterSelectedIndex = selectedIndex;
                Calendar calendar1 = Calendar.getInstance();
                calendar1.set(Calendar.YEAR, years.get(yearSelectedIndex));
                mQuarterPosition = selectedIndex;
            }
        });

        monthWheelView.setOnItemSelectedListener(new WheelView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int selectedIndex, String item) {
                monthSelectedIndex = selectedIndex;
                flushDate();
            }
        });

        weekWheelView.setOnItemSelectedListener(new WheelView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int selectedIndex, String item) {
                weekSelectedIndex = selectedIndex;
                Calendar calendar1 = Calendar.getInstance();
                calendar1.set(Calendar.YEAR, years.get(yearSelectedIndex));
                mWeekPosition = selectedIndex;
            }
        });

        dayWheelView.setOnItemSelectedListener(new WheelView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int selectedIndex, String item) {
                daySelectedIndex = selectedIndex;
                calendar.set(Calendar.DAY_OF_MONTH, days.get(daySelectedIndex));
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dateDialog.dismiss();
            }
        });

        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseDateListener.getDateListener(tag, years.get(yearSelectedIndex), quarters.get(quarterSelectedIndex) + 1,
                        months.get(monthSelectedIndex) + 1, weeks.get(weekSelectedIndex) + 1, days.get(daySelectedIndex));
            }
        });

        Window win = dateDialog.getWindow();
        if (null != win) {
            win.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            // lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
        }
        dateDialog.show();
    }

    public void setDayHint() {
        if (dayWheelView != null) {
            dayWheelView.setVisibility(View.GONE);
        }
    }

    public void setWeekHint() {
        if (null != weekWheelView) {
            weekWheelView.setVisibility(View.GONE);
        }
    }

    public void setMonthHint() {
        if (dayWheelView != null) {
            monthWheelView.setVisibility(View.GONE);
        }
    }

    public void setQuarterHint() {
        if (null != quarterWheelView) {
            quarterWheelView.setVisibility(View.GONE);
        }
    }

    public void setDayShow() {
        if (null != dayWheelView) {
            dayWheelView.setVisibility(View.VISIBLE);
        }
    }

    public void setWeekShow() {
        if (null != weekWheelView) {
            weekWheelView.setVisibility(View.VISIBLE);
        }
    }

    public void setMonthShow() {
        if (null != dayWheelView) {
            monthWheelView.setVisibility(View.VISIBLE);
        }
    }

    public void setQuarterShow() {
        if (null != quarterWheelView) {
            quarterWheelView.setVisibility(View.VISIBLE);
        }
    }

    public void dismiss() {
        if (dateDialog != null) {
            dateDialog.dismiss();
        }
    }

    private void initData() {
        calendar = Calendar.getInstance();
        if (selectedDate == null) {
            selectedDate = new Date();
        }
        calendar.setTime(new Date());
        int startYear = calendar.get(Calendar.YEAR);
        calendar.setTime(selectedDate);
        for (int i = 0; i < ConstantsPool.CHOOSE_DATE_MAX_YEARS; i++) {
            years.add(startYear - i);
            yearTexts.add((startYear - i) + "年");
        }
        for (int i = 1; i < 5; i++) {
            quarters.add(i - 1);
            quarterTexts.add((i) + "季度");
        }
        for (int i = 1; i <= 12; i++) {
            months.add(i - 1);
            monthTexts.add((i) + "月");
        }

        for (int i = 1; i < 53; i++) {
            weeks.add(i - 1);
            weekTexts.add((i) + "周");
        }
        flushDayData();
    }

    /**
     * 获取被选择年的起始位置
     *
     * @return
     */
    private int getYearSelectedIndex() {
        if (selectedDate == null) {
            yearSelectedIndex = 0;
            return 0;
        }
        int year = calendar.get(Calendar.YEAR);

        for (int i = 0; i < years.size(); i++) {
            if (years.get(i) == year) {
                yearSelectedIndex = i;
                break;
            }
        }
        return yearSelectedIndex;
    }

    /**
     * 获取被选择季的起始位置
     */
    private int getQuarterSelectedIndex() {
        if (mQuarterPosition != 0) {
            quarterSelectedIndex = mQuarterPosition;
        } else {
            quarterSelectedIndex = 1;
        }
        return quarterSelectedIndex - 1;
    }

    /**
     * 获取被选择月的起始位置
     *
     * @return
     */
    private int getMonthSelectedIndex() {
        if (selectedDate == null) {
            monthSelectedIndex = 0;
            return 0;
        }
        int month = calendar.get(Calendar.MONTH);

        for (int i = 0; i < months.size(); i++) {
            if (months.get(i) == month) {
                monthSelectedIndex = i;
                break;
            }
        }
        return monthSelectedIndex;
    }

    /**
     * 获取被选择周的起始位置
     *
     * @return
     */
    private int getWeekSelectedIndex() {
        if (null == selectedDate) {
            weekSelectedIndex = 0;
            return 0;
        }

        if (mWeekPosition != 0) {
            weekSelectedIndex = mWeekPosition;
        } else {
            int week = calendar.get(Calendar.WEEK_OF_YEAR);
            for (int i = 0; i < weeks.size(); i++) {
                if (weeks.get(i) == week) {
                    weekSelectedIndex = i;
                    break;
                }
            }
        }
        return weekSelectedIndex - 1;
    }

    /**
     * 获取被选择天的起始位置
     *
     * @return
     */
    private int getDaySelectedIndex() {
        if (selectedDate == null) {
            daySelectedIndex = 0;
            return 0;
        }
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        for (int i = 0; i < days.size(); i++) {
            if (days.get(i) == day) {
                daySelectedIndex = i;
                break;
            }
        }
        if (daySelectedIndex >= days.size()) {
            daySelectedIndex = days.size() - 1;
        }
        return daySelectedIndex;
    }

    /**
     * 刷新月份  注意日期是否之前已经超过将要刷新的月份  防止月份串错
     */
    private void flushDate() {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(Calendar.YEAR, years.get(yearSelectedIndex));
        calendar1.set(Calendar.MONTH, months.get(monthSelectedIndex));
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        int maxDay = calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (daySelectedIndex + 1 > maxDay) {
            daySelectedIndex = maxDay - 1;
        }
        calendar1.set(Calendar.DAY_OF_MONTH, days.get(daySelectedIndex));
        calendar = calendar1;
        flushDayData();
        dayWheelView.setItems(dayTexts, getDaySelectedIndex());
    }

    /**
     * 刷新日期的数据
     */
    private void flushDayData() {
        days.clear();
        dayTexts.clear();
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 1; i <= maxDay; i++) {
            days.add(i);
            dayTexts.add(i + "日");
        }
    }

    public interface ChooseDateListener {
        void getDateListener(String tag, int year, int quarter, int month, int week, int day);
    }
}
